package com.google.android.gms.internal;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.view.Surface;
import android.view.TextureView;
import com.google.android.gms.ads.internal.zzbt;
import com.google.android.gms.common.internal.Hide;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Hide
@zzabh
@TargetApi(14)
public final class zzamp extends zzamz implements MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener, MediaPlayer.OnInfoListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnVideoSizeChangedListener, TextureView.SurfaceTextureListener {
    private static final Map<Integer, String> zzdkj = new HashMap();
    private final zzano zzdkk;
    private final boolean zzdkl;
    private int zzdkm = 0;
    private int zzdkn = 0;
    private MediaPlayer zzdko;
    private Uri zzdkp;
    private int zzdkq;
    private int zzdkr;
    private int zzdks;
    private int zzdkt;
    private int zzdku;
    private zzanl zzdkv;
    private boolean zzdkw;
    private int zzdkx;
    /* access modifiers changed from: private */
    public zzamy zzdky;

    static {
        if (Build.VERSION.SDK_INT >= 17) {
            zzdkj.put(-1004, "MEDIA_ERROR_IO");
            zzdkj.put(-1007, "MEDIA_ERROR_MALFORMED");
            zzdkj.put(-1010, "MEDIA_ERROR_UNSUPPORTED");
            zzdkj.put(-110, "MEDIA_ERROR_TIMED_OUT");
            zzdkj.put(3, "MEDIA_INFO_VIDEO_RENDERING_START");
        }
        zzdkj.put(100, "MEDIA_ERROR_SERVER_DIED");
        zzdkj.put(1, "MEDIA_ERROR_UNKNOWN");
        zzdkj.put(1, "MEDIA_INFO_UNKNOWN");
        zzdkj.put(700, "MEDIA_INFO_VIDEO_TRACK_LAGGING");
        zzdkj.put(701, "MEDIA_INFO_BUFFERING_START");
        zzdkj.put(702, "MEDIA_INFO_BUFFERING_END");
        zzdkj.put(800, "MEDIA_INFO_BAD_INTERLEAVING");
        zzdkj.put(801, "MEDIA_INFO_NOT_SEEKABLE");
        zzdkj.put(802, "MEDIA_INFO_METADATA_UPDATE");
        if (Build.VERSION.SDK_INT >= 19) {
            zzdkj.put(901, "MEDIA_INFO_UNSUPPORTED_SUBTITLE");
            zzdkj.put(902, "MEDIA_INFO_SUBTITLE_TIMED_OUT");
        }
    }

    public zzamp(Context context, boolean z, boolean z2, zzanm zzanm, zzano zzano) {
        super(context);
        setSurfaceTextureListener(this);
        this.zzdkk = zzano;
        this.zzdkw = z;
        this.zzdkl = z2;
        this.zzdkk.zza(this);
    }

    private final void zza(float f) {
        if (this.zzdko != null) {
            try {
                this.zzdko.setVolume(f, f);
            } catch (IllegalStateException unused) {
            }
        } else {
            zzahw.zzcz("AdMediaPlayerView setMediaPlayerVolume() called before onPrepared().");
        }
    }

    private final void zzaf(int i) {
        if (i == 3) {
            this.zzdkk.zzto();
            this.zzdlf.zzto();
        } else if (this.zzdkm == 3) {
            this.zzdkk.zztp();
            this.zzdlf.zztp();
        }
        this.zzdkm = i;
    }

    private final void zzaf(boolean z) {
        zzahw.v("AdMediaPlayerView release");
        if (this.zzdkv != null) {
            this.zzdkv.zztc();
            this.zzdkv = null;
        }
        if (this.zzdko != null) {
            this.zzdko.reset();
            this.zzdko.release();
            this.zzdko = null;
            zzaf(0);
            if (z) {
                this.zzdkn = 0;
                this.zzdkn = 0;
            }
        }
    }

    private final void zzsk() {
        zzahw.v("AdMediaPlayerView init MediaPlayer");
        SurfaceTexture surfaceTexture = getSurfaceTexture();
        if (this.zzdkp != null && surfaceTexture != null) {
            zzaf(false);
            try {
                zzbt.zzfb();
                this.zzdko = new MediaPlayer();
                this.zzdko.setOnBufferingUpdateListener(this);
                this.zzdko.setOnCompletionListener(this);
                this.zzdko.setOnErrorListener(this);
                this.zzdko.setOnInfoListener(this);
                this.zzdko.setOnPreparedListener(this);
                this.zzdko.setOnVideoSizeChangedListener(this);
                this.zzdks = 0;
                if (this.zzdkw) {
                    this.zzdkv = new zzanl(getContext());
                    this.zzdkv.zza(surfaceTexture, getWidth(), getHeight());
                    this.zzdkv.start();
                    SurfaceTexture zztd = this.zzdkv.zztd();
                    if (zztd != null) {
                        surfaceTexture = zztd;
                    } else {
                        this.zzdkv.zztc();
                        this.zzdkv = null;
                    }
                }
                this.zzdko.setDataSource(getContext(), this.zzdkp);
                zzbt.zzfc();
                this.zzdko.setSurface(new Surface(surfaceTexture));
                this.zzdko.setAudioStreamType(3);
                this.zzdko.setScreenOnWhilePlaying(true);
                this.zzdko.prepareAsync();
                zzaf(1);
            } catch (IOException | IllegalArgumentException | IllegalStateException e) {
                String valueOf = String.valueOf(this.zzdkp);
                StringBuilder sb = new StringBuilder(36 + String.valueOf(valueOf).length());
                sb.append("Failed to initialize MediaPlayer at ");
                sb.append(valueOf);
                zzahw.zzc(sb.toString(), e);
                onError(this.zzdko, 1, 0);
            }
        }
    }

    /* JADX WARNING: Removed duplicated region for block: B:10:0x0034 A[LOOP:0: B:10:0x0034->B:15:0x0050, LOOP_START] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private final void zzsl() {
        /*
            r8 = this;
            boolean r0 = r8.zzdkl
            if (r0 != 0) goto L_0x0005
            return
        L_0x0005:
            boolean r0 = r8.zzsm()
            if (r0 == 0) goto L_0x005a
            android.media.MediaPlayer r0 = r8.zzdko
            int r0 = r0.getCurrentPosition()
            if (r0 <= 0) goto L_0x005a
            int r0 = r8.zzdkn
            r1 = 3
            if (r0 == r1) goto L_0x005a
            java.lang.String r0 = "AdMediaPlayerView nudging MediaPlayer"
            com.google.android.gms.internal.zzahw.v(r0)
            r0 = 0
            r8.zza((float) r0)
            android.media.MediaPlayer r0 = r8.zzdko
            r0.start()
            android.media.MediaPlayer r0 = r8.zzdko
            int r0 = r0.getCurrentPosition()
            com.google.android.gms.common.util.zze r1 = com.google.android.gms.ads.internal.zzbt.zzes()
            long r1 = r1.currentTimeMillis()
        L_0x0034:
            boolean r3 = r8.zzsm()
            if (r3 == 0) goto L_0x0052
            android.media.MediaPlayer r3 = r8.zzdko
            int r3 = r3.getCurrentPosition()
            if (r3 != r0) goto L_0x0052
            com.google.android.gms.common.util.zze r3 = com.google.android.gms.ads.internal.zzbt.zzes()
            long r3 = r3.currentTimeMillis()
            long r5 = r3 - r1
            r3 = 250(0xfa, double:1.235E-321)
            int r7 = (r5 > r3 ? 1 : (r5 == r3 ? 0 : -1))
            if (r7 <= 0) goto L_0x0034
        L_0x0052:
            android.media.MediaPlayer r0 = r8.zzdko
            r0.pause()
            r8.zzsn()
        L_0x005a:
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: com.google.android.gms.internal.zzamp.zzsl():void");
    }

    private final boolean zzsm() {
        return (this.zzdko == null || this.zzdkm == -1 || this.zzdkm == 0 || this.zzdkm == 1) ? false : true;
    }

    public final int getCurrentPosition() {
        if (zzsm()) {
            return this.zzdko.getCurrentPosition();
        }
        return 0;
    }

    public final int getDuration() {
        if (zzsm()) {
            return this.zzdko.getDuration();
        }
        return -1;
    }

    public final int getVideoHeight() {
        if (this.zzdko != null) {
            return this.zzdko.getVideoHeight();
        }
        return 0;
    }

    public final int getVideoWidth() {
        if (this.zzdko != null) {
            return this.zzdko.getVideoWidth();
        }
        return 0;
    }

    public final void onBufferingUpdate(MediaPlayer mediaPlayer, int i) {
        this.zzdks = i;
    }

    public final void onCompletion(MediaPlayer mediaPlayer) {
        zzahw.v("AdMediaPlayerView completion");
        zzaf(5);
        this.zzdkn = 5;
        zzaij.zzdfn.post(new zzamr(this));
    }

    public final boolean onError(MediaPlayer mediaPlayer, int i, int i2) {
        String str = zzdkj.get(Integer.valueOf(i));
        String str2 = zzdkj.get(Integer.valueOf(i2));
        StringBuilder sb = new StringBuilder(38 + String.valueOf(str).length() + String.valueOf(str2).length());
        sb.append("AdMediaPlayerView MediaPlayer error: ");
        sb.append(str);
        sb.append(":");
        sb.append(str2);
        zzahw.zzcz(sb.toString());
        zzaf(-1);
        this.zzdkn = -1;
        zzaij.zzdfn.post(new zzams(this, str, str2));
        return true;
    }

    public final boolean onInfo(MediaPlayer mediaPlayer, int i, int i2) {
        String str = zzdkj.get(Integer.valueOf(i));
        String str2 = zzdkj.get(Integer.valueOf(i2));
        StringBuilder sb = new StringBuilder(37 + String.valueOf(str).length() + String.valueOf(str2).length());
        sb.append("AdMediaPlayerView MediaPlayer info: ");
        sb.append(str);
        sb.append(":");
        sb.append(str2);
        zzahw.v(sb.toString());
        return true;
    }

    /* access modifiers changed from: protected */
    /* JADX WARNING: Code restructure failed: missing block: B:13:0x0045, code lost:
        if ((r5.zzdkq * r7) > (r5.zzdkr * r6)) goto L_0x0047;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:24:0x006a, code lost:
        if (r1 > r6) goto L_0x0087;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:32:0x0082, code lost:
        if (r1 > r6) goto L_0x0047;
     */
    /* JADX WARNING: Removed duplicated region for block: B:37:0x008e  */
    /* JADX WARNING: Removed duplicated region for block: B:40:0x0099  */
    /* JADX WARNING: Removed duplicated region for block: B:51:? A[RETURN, SYNTHETIC] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public final void onMeasure(int r6, int r7) {
        /*
            r5 = this;
            int r0 = r5.zzdkq
            int r0 = getDefaultSize(r0, r6)
            int r1 = r5.zzdkr
            int r1 = getDefaultSize(r1, r7)
            int r2 = r5.zzdkq
            if (r2 <= 0) goto L_0x0085
            int r2 = r5.zzdkr
            if (r2 <= 0) goto L_0x0085
            com.google.android.gms.internal.zzanl r2 = r5.zzdkv
            if (r2 != 0) goto L_0x0085
            int r0 = android.view.View.MeasureSpec.getMode(r6)
            int r6 = android.view.View.MeasureSpec.getSize(r6)
            int r1 = android.view.View.MeasureSpec.getMode(r7)
            int r7 = android.view.View.MeasureSpec.getSize(r7)
            r2 = 1073741824(0x40000000, float:2.0)
            if (r0 != r2) goto L_0x004f
            if (r1 != r2) goto L_0x004f
            int r0 = r5.zzdkq
            int r0 = r0 * r7
            int r1 = r5.zzdkr
            int r1 = r1 * r6
            if (r0 >= r1) goto L_0x003f
            int r6 = r5.zzdkq
            int r6 = r6 * r7
            int r0 = r5.zzdkr
            int r0 = r6 / r0
            r6 = r0
            goto L_0x0087
        L_0x003f:
            int r0 = r5.zzdkq
            int r0 = r0 * r7
            int r1 = r5.zzdkr
            int r1 = r1 * r6
            if (r0 <= r1) goto L_0x0087
        L_0x0047:
            int r7 = r5.zzdkr
            int r7 = r7 * r6
            int r0 = r5.zzdkq
            int r1 = r7 / r0
            goto L_0x0086
        L_0x004f:
            r3 = -2147483648(0xffffffff80000000, float:-0.0)
            if (r0 != r2) goto L_0x0060
            int r0 = r5.zzdkr
            int r0 = r0 * r6
            int r2 = r5.zzdkq
            int r0 = r0 / r2
            if (r1 != r3) goto L_0x005e
            if (r0 <= r7) goto L_0x005e
            goto L_0x0087
        L_0x005e:
            r7 = r0
            goto L_0x0087
        L_0x0060:
            if (r1 != r2) goto L_0x006f
            int r1 = r5.zzdkq
            int r1 = r1 * r7
            int r2 = r5.zzdkr
            int r1 = r1 / r2
            if (r0 != r3) goto L_0x006d
            if (r1 <= r6) goto L_0x006d
            goto L_0x0087
        L_0x006d:
            r6 = r1
            goto L_0x0087
        L_0x006f:
            int r2 = r5.zzdkq
            int r4 = r5.zzdkr
            if (r1 != r3) goto L_0x007e
            if (r4 <= r7) goto L_0x007e
            int r1 = r5.zzdkq
            int r1 = r1 * r7
            int r2 = r5.zzdkr
            int r1 = r1 / r2
            goto L_0x0080
        L_0x007e:
            r1 = r2
            r7 = r4
        L_0x0080:
            if (r0 != r3) goto L_0x006d
            if (r1 <= r6) goto L_0x006d
            goto L_0x0047
        L_0x0085:
            r6 = r0
        L_0x0086:
            r7 = r1
        L_0x0087:
            r5.setMeasuredDimension(r6, r7)
            com.google.android.gms.internal.zzanl r0 = r5.zzdkv
            if (r0 == 0) goto L_0x0093
            com.google.android.gms.internal.zzanl r0 = r5.zzdkv
            r0.zzh(r6, r7)
        L_0x0093:
            int r0 = android.os.Build.VERSION.SDK_INT
            r1 = 16
            if (r0 != r1) goto L_0x00b0
            int r0 = r5.zzdkt
            if (r0 <= 0) goto L_0x00a1
            int r0 = r5.zzdkt
            if (r0 != r6) goto L_0x00a9
        L_0x00a1:
            int r0 = r5.zzdku
            if (r0 <= 0) goto L_0x00ac
            int r0 = r5.zzdku
            if (r0 == r7) goto L_0x00ac
        L_0x00a9:
            r5.zzsl()
        L_0x00ac:
            r5.zzdkt = r6
            r5.zzdku = r7
        L_0x00b0:
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: com.google.android.gms.internal.zzamp.onMeasure(int, int):void");
    }

    public final void onPrepared(MediaPlayer mediaPlayer) {
        zzahw.v("AdMediaPlayerView prepared");
        zzaf(2);
        this.zzdkk.zzsp();
        zzaij.zzdfn.post(new zzamq(this));
        this.zzdkq = mediaPlayer.getVideoWidth();
        this.zzdkr = mediaPlayer.getVideoHeight();
        if (this.zzdkx != 0) {
            seekTo(this.zzdkx);
        }
        zzsl();
        int i = this.zzdkq;
        int i2 = this.zzdkr;
        StringBuilder sb = new StringBuilder(62);
        sb.append("AdMediaPlayerView stream dimensions: ");
        sb.append(i);
        sb.append(" x ");
        sb.append(i2);
        zzahw.zzcy(sb.toString());
        if (this.zzdkn == 3) {
            play();
        }
        zzsn();
    }

    public final void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i2) {
        zzahw.v("AdMediaPlayerView surface created");
        zzsk();
        zzaij.zzdfn.post(new zzamt(this));
    }

    public final boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
        zzahw.v("AdMediaPlayerView surface destroyed");
        if (this.zzdko != null && this.zzdkx == 0) {
            this.zzdkx = this.zzdko.getCurrentPosition();
        }
        if (this.zzdkv != null) {
            this.zzdkv.zztc();
        }
        zzaij.zzdfn.post(new zzamv(this));
        zzaf(true);
        return true;
    }

    public final void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i2) {
        zzahw.v("AdMediaPlayerView surface changed");
        boolean z = false;
        boolean z2 = this.zzdkn == 3;
        if (this.zzdkq == i && this.zzdkr == i2) {
            z = true;
        }
        if (this.zzdko != null && z2 && z) {
            if (this.zzdkx != 0) {
                seekTo(this.zzdkx);
            }
            play();
        }
        if (this.zzdkv != null) {
            this.zzdkv.zzh(i, i2);
        }
        zzaij.zzdfn.post(new zzamu(this, i, i2));
    }

    public final void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
        this.zzdkk.zzb(this);
        this.zzdle.zza(surfaceTexture, this.zzdky);
    }

    public final void onVideoSizeChanged(MediaPlayer mediaPlayer, int i, int i2) {
        StringBuilder sb = new StringBuilder(57);
        sb.append("AdMediaPlayerView size changed: ");
        sb.append(i);
        sb.append(" x ");
        sb.append(i2);
        zzahw.v(sb.toString());
        this.zzdkq = mediaPlayer.getVideoWidth();
        this.zzdkr = mediaPlayer.getVideoHeight();
        if (this.zzdkq != 0 && this.zzdkr != 0) {
            requestLayout();
        }
    }

    public final void pause() {
        zzahw.v("AdMediaPlayerView pause");
        if (zzsm() && this.zzdko.isPlaying()) {
            this.zzdko.pause();
            zzaf(4);
            zzaij.zzdfn.post(new zzamx(this));
        }
        this.zzdkn = 4;
    }

    public final void play() {
        zzahw.v("AdMediaPlayerView play");
        if (zzsm()) {
            this.zzdko.start();
            zzaf(3);
            this.zzdle.zzsq();
            zzaij.zzdfn.post(new zzamw(this));
        }
        this.zzdkn = 3;
    }

    public final void seekTo(int i) {
        StringBuilder sb = new StringBuilder(34);
        sb.append("AdMediaPlayerView seek ");
        sb.append(i);
        zzahw.v(sb.toString());
        if (zzsm()) {
            this.zzdko.seekTo(i);
            this.zzdkx = 0;
            return;
        }
        this.zzdkx = i;
    }

    public final void setVideoPath(String str) {
        Uri parse = Uri.parse(str);
        zzin zzd = zzin.zzd(parse);
        if (zzd != null) {
            parse = Uri.parse(zzd.url);
        }
        this.zzdkp = parse;
        this.zzdkx = 0;
        zzsk();
        requestLayout();
        invalidate();
    }

    public final void stop() {
        zzahw.v("AdMediaPlayerView stop");
        if (this.zzdko != null) {
            this.zzdko.stop();
            this.zzdko.release();
            this.zzdko = null;
            zzaf(0);
            this.zzdkn = 0;
        }
        this.zzdkk.onStop();
    }

    public final String toString() {
        String name = getClass().getName();
        String hexString = Integer.toHexString(hashCode());
        StringBuilder sb = new StringBuilder(1 + String.valueOf(name).length() + String.valueOf(hexString).length());
        sb.append(name);
        sb.append("@");
        sb.append(hexString);
        return sb.toString();
    }

    public final void zza(float f, float f2) {
        if (this.zzdkv != null) {
            this.zzdkv.zzb(f, f2);
        }
    }

    public final void zza(zzamy zzamy) {
        this.zzdky = zzamy;
    }

    public final String zzsj() {
        String valueOf = String.valueOf(this.zzdkw ? " spherical" : "");
        return valueOf.length() != 0 ? "MediaPlayer".concat(valueOf) : new String("MediaPlayer");
    }

    public final void zzsn() {
        zza(this.zzdlf.getVolume());
    }
}
